package com.lxs.mall.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.lxs.common.validator.group.AddGroupInterface;
import com.lxs.common.validator.group.UpdateGroupInterface;
import com.lxs.common.utils.PageUtils;
import com.lxs.common.utils.R;
import com.lxs.mall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.lxs.mall.product.entity.BrandEntity;


/**
 * 品牌
 *
 * @author lxs
 * @email 1404637866@qq.com
 * @date 2023-05-11 22:27:17
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("/all")
    public R queryAllBrands(){
        BrandEntity brandEntity=new BrandEntity();
        brandEntity.setName("小米");
        return R.ok().put("brands",brandEntity);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Validated(AddGroupInterface.class) @RequestBody BrandEntity brand){
//        if(result.hasErrors()){
//            //提交的数据经过JSR303校验后有非法字段
//            HashMap hashMap = new HashMap();
//            List<FieldError> fieldErrors = result.getFieldErrors();
//            for(FieldError f: fieldErrors){
//                //获取非法数据的field
//                String field= f.getField();
//                String defaultMessage= f.getDefaultMessage();
//                hashMap.put(field,defaultMessage);
//            }
//            return R.error(400,"提交的表单数据不合法").put("data",hashMap);
//        }
		brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated(UpdateGroupInterface.class)@RequestBody BrandEntity brand){
//		brandService.updateById(brand);
        brandService.updateDetail(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
